<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="分类名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入分类名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="分类级别" prop="levelDepth">
        <el-select v-model="queryParams.levelDepth" placeholder="请选择分类级别" clearable>
          <el-option v-for="item in options" :key="item[1]" :label="item[0]" :value="item[1]" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button v-hasPermi="['recruit:post-classify:list']" type="primary" icon="el-icon-search" size="small"
          @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        <el-button v-hasPermi="['recruit:post-classify:add']" type="primary" plain icon="el-icon-plus" size="small"
          @click="handleAdd">新增</el-button>
        <el-button type="info" plain icon="el-icon-sort" size="small" @click="toggleExpandAll">展开/折叠</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" v-if="refreshTable" :data="postClassifyList" row-key="id"
      :default-expand-all="isExpandAll" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column prop="name" label="分类名称" align="center" />
      <el-table-column prop="levelDepth" label="分类级别" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.levelDepth==1">一级分类</el-tag>
          <el-tag v-else-if="scope.row.levelDepth==2" type="success">二级分类</el-tag>
          <el-tag v-else-if="scope.row.levelDepth==3" type="info">三级分类</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center" />
      <el-table-column prop="updateTime" label="更新时间" align="center" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermi="['recruit:post-classify:update']" size="mini" type="text" icon="el-icon-edit"
            @click="handleUpdate(scope.row)">修改</el-button>
          <el-button v-if="scope.row.levelDepth!=3" v-hasPermi="['recruit:post-classify:add']" size="mini" type="text"
            icon="el-icon-plus" @click="handleAdd(scope.row)">新增</el-button>
          <el-button v-hasPermi="['recruit:post-classify:delete']" size="mini" type="text" icon="el-icon-delete"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="上级分类">
              <treeselect @select="getCheckedNode" v-model="form.parentId" :options="postClassifyOptions"
                :normalizer="normalizer" :show-count="true" placeholder="选择上级分类" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="分类名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入分类名称" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listPostClassify,
    getPostClassify,
    delPostClassify,
    addPostClassify,
    updatePostClassify
  } from "@/api/recruit/post-classify";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  export default {
    name: "PostClassify",
    components: {
      Treeselect
    },
    data() {
      return {
        options: [
          ['一级分类', 1],
          ['二级分类', 2],
          ['三级分类', 3]
        ],
        loading: true,
        maxLevel: 0,
        postClassifyList: [],
        postClassifyOptions: [],
        queryParams: {
          name: undefined,
          levelDepth: undefined
        },
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否展开，默认全部折叠
        isExpandAll: false,
        // 重新渲染表格状态
        refreshTable: true,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: {
            required: true,
            message: "分类名称不能为空",
            trigger: "blur"
          }
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询岗位分类列表 */
      getList() {
        this.loading = true;
        listPostClassify(this.queryParams).then(response => {
          this.postClassifyList = response.data;
          this.postClassifyOptions = response.data;
          this.loading = false;
        });
      },
      getCheckedNode(node) {
        this.form.parentId = node.id
        this.form.levelDepth = node.levelDepth + 1
      },
      /** 转换菜单数据结构 */
      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children: node.children
        };
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          parentId: 0,
          name: undefined,
          levelDepth: 1
        };
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.$refs['queryForm'].resetFields();
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset();
        this.form.parentId = row.id
        this.form.levelDepth = row.levelDepth + 1
        this.title = "添加分类";
        this.open = true;
      },
      /** 展开/折叠操作 */
      toggleExpandAll() {
        this.refreshTable = false;
        this.isExpandAll = !this.isExpandAll;
        this.$nextTick(() => {
          this.refreshTable = true;
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.getMaxLevelDepth(row)
        getPostClassify(row.id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改分类";
        });
      },
      getMaxLevelDepth(row) {
        if (row.children != null && row.children.length > 0) {
          for (let i = 0; i < row.children.length; i++) {
            if (row.children[i].levelDepth > this.maxLevel) {
              this.maxLevel = row.children[i].levelDepth;
            }
            this.getMaxLevelDepth(row.children[i]);
          }
        }
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (!this.form.parentId) {
              this.form.parentId = 0
            }
            if (this.form.id != undefined) {
              if (this.maxLevel == 3 && this.form.parentId != 0) {
                this.$message.error('分类最大层级为三级')
                return
              }
              updatePostClassify(this.form).then(response => {
                this.$message.success("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addPostClassify(this.form).then(response => {
                this.$message.success("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$confirm(`此操作将删除【${row.name}】分类, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delPostClassify(row.id).then(res => {
            this.getList();
            this.$message.success("删除成功");
          })
        }).catch(() => {});
      }
    }
  };
</script>
